<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Common extends MX_Controller {  
     
    /* configuration */    		
    function get_post_status($name){
        $status = array(
                'xuat_ban' => 'publish',	
                'cho_duyet' => 'pending',	
                'tu_choi' => 'reject'						
        );	
        return $status[$name];
    }    
    function removesign($str)
    {
        $coDau=array(" ","à","á","?","?","ã","â","?","?","?","?","?","a","?","?"
        ,"?","?","?","è","é","?","?","?","ê","?","?","?","?","?","ì","í","?","?","i",
            "ò","ó","?","?","õ","ô","?","?","?","?","?","o"
        ,"?","?","?","?","?",
            "ù","ú","?","?","u","u","?","?","?","?","?",
            "?","ý","?","?","?",
            "d",
            "À","Á","?","?","Ã","Â","?","?","?","?","?","A"
        ,"?","?","?","?","?",
            "È","É","?","?","?","Ê","?","?","?","?","?",
            "Ì","Í","?","?","I",
            "Ò","Ó","?","?","Õ","Ô","?","?","?","?","?","O"
        ,"?","?","?","?","?",
            "Ù","Ú","?","?","U","U","?","?","?","?","?",
            "?","Ý","?","?","?",
            "Ð","ê","ù","à");
        $khongDau=array("_","a","a","a","a","a","a","a","a","a","a","a"
        ,"a","a","a","a","a","a",
            "e","e","e","e","e","e","e","e","e","e","e",
            "i","i","i","i","i",
            "o","o","o","o","o","o","o","o","o","o","o","o"
        ,"o","o","o","o","o",
            "u","u","u","u","u","u","u","u","u","u","u",
            "y","y","y","y","y",
            "d",
            "A","A","A","A","A","A","A","A","A","A","A","A"
        ,"A","A","A","A","A",
            "E","E","E","E","E","E","E","E","E","E","E",
            "I","I","I","I","I",
            "O","O","O","O","O","O","O","O","O","O","O","O"
        ,"O","O","O","O","O",
            "U","U","U","U","U","U","U","U","U","U","U",
            "Y","Y","Y","Y","Y",
            "D","e","u","a");
        return str_replace($coDau,$khongDau,$str);
    }  
    function removespace($str)
    {
        $coSpace=array("    ","   ","  "," ");
        $khongSpace=array("_","_","_","_");
        return str_replace($coSpace,$khongSpace,$str);
    }
    function remove_disallow($str)
    {
        $co=array("!","*","'","(",")",";",":","@","&","=","+","$",",","/","?","%","#","[","]","\"");
        $khong=array(" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ");
        return str_replace($co,$khong,$str);
    }
    function truncateString_($str, $len, $charset="UTF-8"){
       $str = html_entity_decode($str, ENT_QUOTES, $charset);   
       if(mb_strlen($str, $charset)> $len){
           $arr = explode(' ', $str);
           $str = mb_substr($str, 0, $len, $charset);
           $arrRes = explode(' ', $str);
           $last = $arr[count($arrRes)-1];
           unset($arr);
           if(strcasecmp($arrRes[count($arrRes)-1], $last))   unset($arrRes[count($arrRes)-1]);
         return implode(' ', $arrRes);   
      }
       return $str;
    }
    function get_link($str = '', $table = '')
    {
                
        $max_length = 100;
        if  (strlen($str)>$max_length)
        {
            $link = $this->truncateString_($str,$max_length);
        } else $link = $str;
        $link =$this->remove_disallow($link);
        $link =$this->removespace($link);
        $link = urlencode($link);
        if ($this->check_link($link,$table)==false)
        {
            return $link;
        }
        $i = 0;
        while (1)
        {	
            $i++;
            $link_temp = $link.'_'.$i;
            if ($this->check_link($link_temp,$table)==false) return $link_temp;
        }
        
    }		
    function check_link($link = '',$table = '')
    {
        //co thi la true ko co là false
        $object = new $table();
        $sl = $object->where('slug',$link)->count();
        if ($sl>0)
        {
            return true;
        } else return false;
        
    }
    function get_lang($str = '', $lang_code = 'vi')
    {        
        preg_match('/<!--:'.$lang_code.'-->(.*?)<!--:-->/',$str,$arr);
        if (count($arr) > 0) { return $arr[1]; } else { return $str;}
    }   
    function load_template($view = '',$data)
    {          	
        $option = new Option();
        $option->where('option_name','template')->get();
        $theme = $option->option_value;       

        if(get_file_info('application/views/front_end/'.$theme.'/'.$view.'.php'))
        {
            $data['view'] = "front_end/".$theme."/".$view;
        }
        else {
            $data['view'] = "front_end/base/".$view;
        }
    
        if(get_file_info('application/views/front_end/'.$theme.'/template.php'))
        {            
            $this->load->view('front_end/'.$theme.'/template',$data);
        }
        else 
        {
            $this->load->view('front_end/base/template');
        }
     }      
}